package com.fenrirtec.aepp.common.api;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.fenrirtec.aepp.common.condition.MemberCondition;
import com.fenrirtec.aepp.common.dto.TenderEnrollListDto;
import com.fenrirtec.aepp.common.service.TenderEnrollService;
import com.fenrirtec.aepp.common.service.impl.TenderEnrollServiceImpl;
import com.fenrirtec.aepp.common.support.JSONActionSupport;
import com.fenrirtec.core.definition.BaseDefinition.API;
import com.fenrirtec.core.exception.DatabaseException;
import com.fenrirtec.core.exception.UnexpectedException;
import com.fenrirtec.core.utils.JsonUtils;

public class TenderEnrollSearchAPI extends JSONActionSupport {
	private static final long serialVersionUID = 1L;

	private static final Logger logger = LoggerFactory.getLogger(TenderEnrollSearchAPI.class);

	public TenderEnrollSearchAPI() {
	}
	
	private String param;
    private int _page;	
	private int _rows;
	
	public String getParam() {
		return this.param;
	}

	public void setParam(String param) {
		this.param = param;
	}

	public int get_page() {
		return _page;
	}

	public void set_page(int _page) {
		this._page = _page;
	}

	public int get_rows() {
		return _rows;
	}

	public void set_rows(int _rows) {
		this._rows = _rows;
	}
	
	@Override
	protected List<ErrorInfo> doValidate() {
		return null;
	}

	@Override
	protected JSONObject doExecute() {
		TenderEnrollService service = new TenderEnrollServiceImpl();
		List<TenderEnrollListDto> tenderEnrollDtoList = null;
		Integer tenderEnrollCount = 0;
		
		try {
			JSONObject jsonObj = JSONObject.parseObject(this.param);
			MemberCondition condition = new MemberCondition();
			
			condition = JsonUtils.toBean(jsonObj, MemberCondition.class);
			
            condition.setPage(_page);
            condition.setRows(_rows);
           
			tenderEnrollDtoList = service.searchByCnd(condition);
		} catch (DatabaseException e) {
			if (logger.isErrorEnabled()) {
				logger.error("database error occurred.", e);
			}
			throw new JSONActionStatusException(API.Common.Status.DB_ERROR, e);
		} catch (UnexpectedException e) {
			if (logger.isErrorEnabled()) {
				logger.error("unexpected error occurred.", e);
			}
			throw new JSONActionStatusException(API.Common.Status.DB_ERROR, e);
		}
		tenderEnrollCount = tenderEnrollDtoList.size();
		JSONObject root = new JSONObject();
		if(tenderEnrollCount !=null && tenderEnrollCount > 0){
			root.put("recordsTotal", tenderEnrollCount);
            root.put("recordsFiltered", tenderEnrollCount);
            root.put("total", tenderEnrollCount);
		}else{
			root.put("recordsTotal", 0);
            root.put("recordsFiltered", 0);      
            root.put("total", 0);
		}
              
		if (tenderEnrollDtoList != null && tenderEnrollDtoList.size() > 0) {
			for(int i=0;i<tenderEnrollDtoList.size();i++){
				if(tenderEnrollDtoList.get(i).getFinalistFlag().equals(true)){
					tenderEnrollDtoList.get(i).setStrfinalistFlag("是");
				}else{
					tenderEnrollDtoList.get(i).setStrfinalistFlag("否");
				}
				Integer tenderId=tenderEnrollDtoList.get(i).getTenderId();
				if(tenderEnrollDtoList.get(i).getStatus().equals(4)){					
					tenderEnrollDtoList.get(i).setTenderPatternName("<a id='btn_search_enrollinfo' class='button_edit' onclick='getTenderId("+tenderId+")' href='#'>查看</a>&nbsp;<a id='btn_enroll' class='button_enroll' onclick='getTenderId("+tenderId+")' href='#'>投标</a>");
				}else{
					tenderEnrollDtoList.get(i).setTenderPatternName("<a id='btn_search_enrollinfo' class='button_edit' onclick='getTenderId("+tenderId+")' href='#'>查看</a>");
				}
			}			 
			root.put("tenderEnrolls", JsonUtils.fromBean(tenderEnrollDtoList));
		}
		getSession().remove("prePage");
		return root;
	}
}
